from selenium import webdriver
import json
option = webdriver.ChromeOptions()
option.headless = True
plantname = "玉米"
driver = webdriver.Chrome(options=option)
driverpic = webdriver.Chrome(options=option)
driver.get("http://www.plant.csdb.cn/photo?sname=&chname={}&creator=&province=&loc=&habit=&year=".format(plantname));
numlable = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/font/b')
num = int(numlable.text)
page = int(num / 50)
piclist = []
for p in range(0, page + 1):
    driver.get('http://www.plant.csdb.cn/photo?page={}&sname=&chname={}&creator=&province=&loc=&habit=&year='.format(p, plantname))
    for x in range(1, 11):
        for y in range(1, 6):
            if (len(piclist) < num):
                pic = driver.find_element_by_xpath('// *[ @ id = "content"] / div[2] / div / div[1] / table / tbody / tr[{}] / td[{}] / a[1]'.format(x, y))
                driverpic.get(pic.get_attribute("href"))
                src = driverpic.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/img').get_attribute("src")
                piclist.append(src)
                print(len(piclist))
with open(plantname + '.json', 'w') as f:
   json.dump(piclist, f)
driver.quit()
driverpic.quit()
